package com.isseiaoki.simplecropview.animation;


import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;

public class ValueAnimatorV14
        implements SimpleValueAnimator, AnimatorValue.StateChangedListener,
        AnimatorValue.ValueUpdateListener {
    private static final int DEFAULT_ANIMATION_DURATION = 150;
    private AnimatorValue animator;
    private SimpleValueAnimatorListener animatorListener = new SimpleValueAnimatorListener() {
        @Override
        public void onAnimationStarted() {

        }

        @Override
        public void onAnimationUpdated(float scale) {

        }

        @Override
        public void onAnimationFinished() {

        }
    };

    public ValueAnimatorV14(){
        animator=new AnimatorValue();
        animator.setValueUpdateListener(this);
        animator.setStateChangedListener(this);
    }


    @Override
    public void startAnimation(long duration) {
        if (duration >= 0) {
            animator.setDuration(duration);
        } else {
            animator.setDuration(DEFAULT_ANIMATION_DURATION);
        }
        animator.start();
    }

    @Override
    public void cancelAnimation() {
        animator.cancel();
    }

    @Override
    public boolean isAnimationStarted() {
        return animator.isRunning();
    }

    @Override
    public void addAnimatorListener(SimpleValueAnimatorListener animatorListener) {
        if (animatorListener != null) this.animatorListener = animatorListener;
    }

    @Override
    public void onStart(Animator animator) {
        animatorListener.onAnimationStarted();
    }

    @Override
    public void onStop(Animator animator) {
        animatorListener.onAnimationFinished();
    }

    @Override
    public void onCancel(Animator animator) {
        animatorListener.onAnimationFinished();
    }

    @Override
    public void onEnd(Animator animator) {
        animatorListener.onAnimationFinished();
    }

    @Override
    public void onPause(Animator animator) {
        animatorListener.onAnimationFinished();
    }

    @Override
    public void onResume(Animator animator) {
        animatorListener.onAnimationStarted();
    }

    @Override
    public void onUpdate(AnimatorValue animatorValue, float v) {
        animatorListener.onAnimationUpdated(v);
    }

//  @Override public void onAnimationStart(Animator animation) {
//    animatorListener.onAnimationStarted();
//  }
//
//  @Override public void onAnimationEnd(Animator animation) {
//    animatorListener.onAnimationFinished();
//  }
//
//  @Override public void onAnimationCancel(Animator animation) {
//    animatorListener.onAnimationFinished();
//  }
//
//  @Override public void onAnimationRepeat(Animator animation) {
//
//  }
//
//  @Override public void onAnimationUpdate(ValueAnimator animation) {
//    animatorListener.onAnimationUpdated(animation.getAnimatedFraction());
//  }
}
